package main

import "fmt"

type Test struct {
	name string
}

func (t *Test) close() {
	fmt.Println(t.name, " closed")
}

// defer执行时，注意对象不能重赋值，不然可能出现意想不到的执行结果
func main1() {
	ts := []Test{{"a"}, {"b"}, {"c"}}
	for _, t := range ts {
		defer t.close()
	}
	defer fmt.Println("------------------")
	for _, t := range ts {
		t2 := t
		defer t2.close()
	}
	defer fmt.Println("------------------")
	var t2 Test
	for _, t := range ts {
		t2 = t
		defer t2.close()
	}
	fmt.Println("over")
}
